
file_extension_to_open = ".fits";

// Path on my Laptop
pathToImg   = "C:\\Users\\Jason\\Dropbox\\School\\PILOT\\_htdocs\\imagerecon\\Images\\Samples\\";
//saveImgPath = "/home/jfrank3/www-home/imagerecon/Images/";
//saveImgName = "stack_from_resampled_RAW.zip";
//saveImgType = "ZIP";
scale       = 2;


// Get a list of all files in dir
images_to_open = getFileList(pathToImg);


// Open all imgs
for( i = 0; i < images_to_open.length; i++ ) {
	if(endsWith(images_to_open[i], file_extension_to_open)) {
				open(pathToImg + images_to_open[i]);
	}
}


/*
// Brighten images so resultant stack will be legible
for( i = 0; i < images_to_open.length; i++ ) {
	if(endsWith(images_to_open[i], file_extension_to_open)) {
		selectWindow(images_to_open[i]);


		run("Brightness/Contrast...");
		setMinAndMax(1150, 3650);
	}
}
*/




// Downsample imgs (in prep of sending the stack to the client for previewing)
for( i = 0; i < images_to_open.length; i++ ) {
	if(endsWith(images_to_open[i], file_extension_to_open)) {
		selectWindow(images_to_open[i]);
		      //run("Resample", "factor=4 factor=4 factor=4");
		run("Resample", "factor=" + scale + " factor=" + scale + " factor=" + scale);
		/*
		// Brighten images AGAIN (since downsampling darkens them) so resultant stack will be legible
		run("Brightness/Contrast...");
		setMinAndMax(1150, 3650);
		*/
	}
}



// Create the stack from all open images - first close the orig (full-sized) img windows so we don't stack them
for( i = 0; i < images_to_open.length; i++ ) {
	if(endsWith(images_to_open[i], file_extension_to_open)) {
		selectWindow(images_to_open[i]);
		close();
	}
}

run("Images to Stack", "name=[] title=[] use keep");
